package com.syscom.common.utils.generator.java;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.generator.AutoGenerator;
import com.baomidou.mybatisplus.generator.InjectionConfig;
import com.baomidou.mybatisplus.generator.config.GlobalConfig;
import com.baomidou.mybatisplus.generator.config.PackageConfig;
import com.baomidou.mybatisplus.generator.config.StrategyConfig;
import com.baomidou.mybatisplus.generator.config.TemplateConfig;
import com.baomidou.mybatisplus.generator.config.po.TableInfo;
import com.baomidou.mybatisplus.generator.engine.FreemarkerTemplateEngine;
import com.syscom.common.utils.TextUtil;
import com.syscom.common.utils.generator.tool.GeneratorTool;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.Map;

/**
 * @author wjz
 * @date 2021/1/4 12:03
 * @Description
 */
@Component
public class AdminGenerator {

    @Autowired
    private GeneratorTool generatorTool;

    /**
     * admin模块代码自动生成
     */
    public Map adminCodeGenerator(String moduleName, String parent, String tableName, JSONObject queryObj) {
        final Map result = new HashMap();
        // 代码生成器
        AutoGenerator mpg = new AutoGenerator();

        String projectPath = System.getProperty("user.dir");
        GlobalConfig gc = generatorTool.getGlobalConfig(projectPath, "admin");
        gc.setServiceName("%sService");
        mpg.setGlobalConfig(gc);
        mpg.setDataSource(generatorTool.getDataSourceConfig());
        PackageConfig pc = generatorTool.getPackageConfig(moduleName, parent);
        mpg.setPackageInfo(pc);
        // 自定义配置
        InjectionConfig cfg = new InjectionConfig() {
            @Override
            public void initMap() {
                // to do nothing
            }

            @Override
            public Map<String, Object> prepareObjectMap(Map<String, Object> objectMap) {
                TableInfo table = (TableInfo) objectMap.get("table");
                String entityName = table.getEntityName();
                //加入mapper文件的首字母小写名称
                objectMap.put("lowerMapperName", TextUtil.firstToLowerCase(entityName) + "Mapper");
                //加入entityName的首字母小写名称
                objectMap.put("lowerEntityName", TextUtil.firstToLowerCase(entityName));
                objectMap.put("queryObj", queryObj);
                result.putAll(objectMap);
                return super.prepareObjectMap(objectMap);
            }
        };
        mpg.setCfg(cfg);
        // 策略配置
        StrategyConfig strategy = generatorTool.getStrategyConfig(tableName);

        // 配置模板 指定自定义模板路径，注意不要带上.ftl/.vm, 会根据使用的模板引擎自动识别
        TemplateConfig templateConfig = new TemplateConfig();
        //不生成xml目录及文件
        templateConfig.setXml("");
        //不生成impl目录及文件
        templateConfig.setServiceImpl("");
        //不生成entity目录及文件
        templateConfig.setEntity("");
        //不生成mapper目录及文件
        templateConfig.setMapper("");

        templateConfig.setController("template/adminController.java");
        templateConfig.setService("template/adminService.java");

        mpg.setTemplate(templateConfig);
        mpg.setStrategy(strategy);
        // 选择 freemarker 引擎需要指定如下加，注意 pom 依赖必须有！
        mpg.setTemplateEngine(new FreemarkerTemplateEngine());
        mpg.execute();
        return result;
    }
}
